package org.ruiyichen.guizhou.agr.ui.mineview.html5;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import org.ruiyichen.guizhou.agr.R;

import androidx.core.content.ContextCompat;


/**
 * 作者：Administrator
 * 邮箱：wutian8489@foxmail.com
 */
public class H5WebView extends WebView {

    public static final String PHONE_TAG = "tel:";

    //进度条高度
    private final int PROGRESSBAR_HEIGHT = 10;
    //进度条显示高度--实际高度
//    private final int HEIGHT = 5;


    //进度条
    private ProgressBar progressbar;

//    private Context mContext;

    public H5WebView(Context context) {
        this(context, null);
    }

    public H5WebView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.webViewStyle);
    }

    public H5WebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        progressbar = new ProgressBar(context, attrs, android.R.attr.progressBarStyleHorizontal);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            progressbar.setProgressDrawable(context.getDrawable(R.drawable.myprogress_style));
//        } else {
//            progressbar.setProgressDrawable(context.getDrawable(R.drawable.myprogress_style));
//        }
        progressbar.setProgressDrawable(ContextCompat.getDrawable(context, R.drawable.myprogress_style));
//        progressbar.setIndeterminateDrawable();
//        progressbar.setBackgroundColor(R.color.blue);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, PROGRESSBAR_HEIGHT, 0, -PROGRESSBAR_HEIGHT / 2));
        addView(progressbar);

        this.getSettings().setDefaultTextEncodingName("utf-8");
        //add
        // Configure the webview
        //网页缩放按钮，销毁时容易挂机。
        this.getSettings().setBuiltInZoomControls(false);

        this.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        this.getSettings().setUseWideViewPort(true);
        this.getSettings().setLoadWithOverviewMode(true);
        this.getSettings().setSavePassword(true);
        this.getSettings().setSaveFormData(true);
        this.getSettings().setJavaScriptEnabled(true);
        this.getSettings().setBlockNetworkImage(false);

        this.requestFocus();

        this.setScrollBarStyle(SCROLL_AXIS_NONE);


        // enable Web Storage: localStorage, sessionStorage
        this.getSettings().setDomStorageEnabled(true);
        // enable navigator.geolocation
        this.getSettings().setGeolocationEnabled(true);
//        this.getSettings().setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");
        this.getSettings().setGeolocationDatabasePath(context.getDir("database", Context.MODE_PRIVATE).getPath());


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getSettings().setLoadsImagesAutomatically(true);
        } else {
            this.getSettings().setLoadsImagesAutomatically(false);
        }

        //下载监听

//        this.setDownloadListener(webDownLoadListener = new WebDownLoadListener(this));
        //位置
//        this.setWebChromeClient(new H5GeoClient());
    }

    /**
     * 设置H5WebChromeClient
     *
     * @param client
     */
    public void setH5WebChromeClient(H5GeoClient client) {
        //绑定进度条
        client.bindProgressBar(progressbar);
        this.setWebChromeClient(client);
    }

    @Override
    public void setWebChromeClient(WebChromeClient client) {
        super.setWebChromeClient(client);
    }

    public ProgressBar getProgressbar() {
        return progressbar;
    }

}
